package com.atlin.project.service.impl;

import com.atlin.project.common.ErrorCode;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.model.domain.BiTeam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atlin.project.model.domain.BiUserTeam;
import com.atlin.project.service.BiUserTeamService;
import com.atlin.project.mapper.BiUserTeamMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author atlin
 * @description 针对表【bi_user_team(用户团队映射表)】的数据库操作Service实现
 * @createDate 2025-02-16 20:44:50
 */
@Service
public class BiUserTeamServiceImpl extends ServiceImpl<BiUserTeamMapper, BiUserTeam>
        implements BiUserTeamService {

    @Override
    public boolean saveUserTeam(long userId, long teamId) {
        // 校验
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getTeamId, teamId).eq(BiUserTeam::getUserId, userId);
        BiUserTeam oldBiUserTeam = getOne(wrapper);
        if (oldBiUserTeam != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "已加入该团队");
        }

        BiUserTeam biUserTeam = new BiUserTeam();
        biUserTeam.setUserId(userId);
        biUserTeam.setTeamId(teamId);

        return save(biUserTeam);
    }

    @Override
    public boolean leaveUserTeam(long userId, long teamId) {
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getTeamId, teamId).eq(BiUserTeam::getUserId, userId);
        return remove(wrapper);
    }

    @Override
    public boolean deleteTeamUser(long teamId) {
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getTeamId, teamId);
        return remove(wrapper);
    }

    @Override
    public List<BiUserTeam> listUserTeamByTeamId(long teamId) {
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getTeamId, teamId);
        return list(wrapper);
    }

    @Override
    public List<BiUserTeam> listUserTeamByUserId(long userId) {
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getUserId, userId);
        return list(wrapper);
    }

    @Override
    public BiUserTeam getUserTeam(Long userId, Long teamId) {
        LambdaQueryWrapper<BiUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BiUserTeam::getUserId, userId).eq(BiUserTeam::getTeamId, teamId);
        return getOne(wrapper);
    }
}




